﻿@using System.Linq;
@using System.Data;
@using System.Text.RegularExpressions;

@{
  Layout = "~/_SiteLayout.cshtml";

  var isAllowed = (bool?) Session["IsAllowed"];
  if (isAllowed == null)
  {
    Response.Redirect("~/Account/Register.cshtml");
  }
  else if (!isAllowed.Value)
  {
    Response.Redirect("~/Account/AccountLockedOut.cshtml");
  }
}

<!-- note: switching to method="GET" would cause the *SECOND* request after a fresh reset to completely hang up in la-la land  -->
<form id="inputsForm" action="@Href("~/WebMethods/ReportQuery.cshtml")" method="POST">
  <!--input name="xml" type="hidden" value="1" /--> <!-- to debug the raw xml data before xsl-transform to html is applied -->
  <div class="input">
    Report:
    @{
      var rgx = new Regex("(?<!^)([A-Z][a-z]|(?<=([a-z]|-))([A-Z]|[0-9]))", RegexOptions.Compiled);

      using (var ReportList = new Proc("ReportList"))
      {
        @Html.DropDownList("ReportName", "(select)",
                                      ReportList.Table0.Rows.Cast<DataRow>().Select(
                                   r => new SelectListItem()
                                          {
                                            Text = rgx.Replace(r.Field<string>("specific_name").Replace("GL_REPORT_", ""), " $1"),
                                            Value = r.Field<string>("parms")
                                          }
                                   )
                 );
      }
    }
  </div>
  <div class="input" id="divInputOfficeCode">
    Tax Office:
    @using (var TaxOffice_s = new Proc("TaxOffice_s"))
    {
      @Html.DropDownList("OfficeCode",
                               new SelectListItem[] { new SelectListItem() { Text = "(All)", Value = "" } }.Union(
                                 TaxOffice_s.Table0.Rows.Cast<DataRow>().
                                   Select(
                                     r => new SelectListItem() { Text = r.Field<string>("TaxOfficeName"), Value = r.Field<string>("OfficeCode") }
                                   ))
               );
    }
  </div>
  <div class="input" id="divInputCCode">
    CCode: @Html.TextBox("CCode")</div>
  <div class="input" id="divInputLastName">
    Last Name: @Html.TextBox("LastName")</div>
  <div class="input" id="divInputFirstName">
    First Name: @Html.TextBox("FirstName")</div>
  <div class="input" id="divInputOrderNumber">
    Order Number: @Html.TextBox("OrderNumber")</div>
  <div class="input" id="divInputBeginDate">
    Begin Date (m/d/yyyy): @Html.TextBox("BeginDate")&nbsp;&nbsp;
    End Date: @Html.TextBox("EndDate")&nbsp;
    Quick select: <select id="EasyDates">
                    <option></option>
                    <option>Last Week</option>
                    <option>This Month To Date</option>
                    <option>Last Month</option>
                    <option>Last Month To Date</option>
                    <option>Fiscal Year To Date</option>
                    <option>Last Fiscal Year</option>
                  </select>
  </div>
  <div class="input"><input id="submit" type="submit" value="Submit" /></div>
  <div class="input" id="reportOutput">
  </div>
</form>

<script type="text/javascript">
  function DateToString(date) {
    return ((date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear());
  }

  function onChangeReport() {
    $('#reportOutput').html("");
    $('#EasyDates').selectedIndex = 0;

    var procParms = $("#ReportName").val();
    $("[id^=divInput]").each(function (index, element) {
      if (procParms.indexOf(element.id.replace("divInput", "")) > -1) $(element).fadeIn();
      else $(element).fadeOut();
    });
  }

  $(document).ready(function () {
    $("#BeginDate").datepicker();
    $("#EndDate").datepicker();
    $("#ReportName").on("change", onChangeReport);
    onChangeReport();

    $("#EasyDates").on("change", function () {
      var today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 0, 0, 0);

      var lastMonday = new Date(today);
      lastMonday.setDate(today.getDate() - ((today.getDay() - 1) % 7) - 7);
      var lastSunday = new Date(lastMonday);
      lastSunday.setDate(lastMonday.getDate() + 6);

      var lastmonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
      var endlastmonth = new Date(today.getFullYear(), today.getMonth(), 0);
      
      var thismonth = new Date(today.getFullYear(), today.getMonth(), 1);

      var fiscal = new Date(today.getFullYear(), 10 - 1, 1);
      if (fiscal > today) fiscal.setYear(today.getFullYear() - 1);

      var lastfiscal = new Date(fiscal);
      lastfiscal.setYear(lastfiscal.getFullYear() - 1);

      switch ($("#EasyDates").val()) {
        case "": $("#BeginDate").val(""); $("#EndDate").val(""); break;
        case "Last Week": $("#BeginDate").val(DateToString(lastMonday)); $("#EndDate").val(DateToString(lastSunday)); break;
        case "This Month To Date": $("#BeginDate").val(DateToString(thismonth)); $("#EndDate").val(DateToString(today)); break;
        case "Last Month": $("#BeginDate").val(DateToString(lastmonth)); $("#EndDate").val(DateToString(endlastmonth)); break;
        case "Last Month To Date": $("#BeginDate").val(DateToString(lastmonth)); $("#EndDate").val(DateToString(today)); break;
        case "Fiscal Year To Date": $("#BeginDate").val(DateToString(fiscal)); $("#EndDate").val(DateToString(today)); break;
        case "Last Fiscal Year": $("#BeginDate").val(DateToString(lastfiscal)); $("#EndDate").val(DateToString(fiscal)); break;
      }
    });

    $("#submit").throbber("click", { image: '@Href("~/js/throbber.gif")' });

    $('#inputsForm').submit(function () {
      $("#reportOutput").html(""); //blank out the previous result

      var endDate = new Date($("#EndDate").val());
      endDate.setHours(23, 59, 59); //to pull in the full day, set the enddate's time to 23:59

      $.ajax({
        url: this.action,
        type: this.method,
        data: {
          ReportName: "GL_REPORT_" + $("#ReportName option:selected").text().replace(/ /g, ""),
          OfficeCode: $("#OfficeCode").val(),
          CCode: $("#CCode").val(),
          LastName: $("#LastName").val(),
          FirstName: $("#FirstName").val(),
          OrderNumber: $("#OrderNumber").val(),
          BeginDate: $("#BeginDate").val(),
          EndDate: endDate.toLocaleString()
        },
        complete: function (result) {
          $.throbberHide(); //according to docs, hide should be automatic with ajax: http://archive.plugins.jquery.com/project/throbber
          $("#reportOutput").html(result.responseText);
        }
      });
      return false;
    });

  });
  
</script>
